package com.duyj.server.order.service.impl;

import com.duyj.client.account.feign.AccountFeignService;
import com.duyj.client.account.model.Account;
import com.duyj.client.order.model.Order;
import com.duyj.server.order.dao.OrderDAO;
import com.duyj.server.order.service.OrderService;
import io.seata.core.context.RootContext;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl implements OrderService {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private OrderDAO orderDAO;

    @Autowired
    private AccountFeignService accountFeignService;

    @Override
    public Order getOrder(Long id) {
        return orderDAO.getOrder(id);
    }

    @Override
    @Transactional
    public void postOrder(Order order) {
        logger.info("Service Begin ... xid: " + RootContext.getXID());
        orderDAO.insertOrder(order);
        Account account = new Account();
        account.setAmount(order.getAmount());
        account.setName(order.getUserName());
        accountFeignService.updateAccount(account);
    }
}
